$(function(){
	findType();
	findFirm();
});
function findType(){
	$("#typeSelect").html("");
	$("#typeId").html("");
	var url="../../matype/query.action";
	var data=null;
	$.post(url,data,function(mes){
		$("#typeId").append("<option value=''>====请选择====</option>");
		$("#typeSelect").append("<option value=''>====请选择====</option>");
		$.each(mes,function(index,item){
			$("#typeId").append("<option value="+item.typeId+">"+item.typeName+"</option>");
			$("#typeSelect").append("<option value="+item.typeId+">"+item.typeName+"</option>");
			load();
		});
	},"json");
}
function findFirm(){
	$("#firmId").html("");
	var url="../../firm/query.action";
	var data=null;
	$.post(url,data,function(mes){
		$("#firmId").append("<option value=''>====请选择====</option>");
		$.each(mes,function(index,item){
			$("#firmId").append("<option value="+item.firmId+">"+item.firmName+"</option>");
			load();
		});
	},"json");
}

function exit(){
	$("#qualityBody").val("");
	$("#maNameInput").val("");
	$("#maMoney").val("");
	$("#maUnit").val("");
	$("#firmId").val("");
	$("#typeId").val("");
	//关闭所有页面层
	layer.closeAll('page');
	load();
}
layui.use(['form', 'layedit', 'laydate','table'], function(){
	var table = layui.table
	,laydate = layui.laydate
	,form = layui.form
	,layer = layui.layer
	,layedit = layui.layedit;

	//日期
	laydate.render({
		elem: '#date'
	});

	laydate.render({
		elem: '#time'
	});
	//监听工具条
	table.on('tool(demo)', function(obj){
		var da = obj.data;

		if(obj.event === 'cxdetail'){
			layer.open({
				type: 1	 //此处以iframe举例
				,title: '查看质检详情'
					,area: ['1000px', '490px']
			,offset: 'auto'
				,content:$('#qualityDiv')
				,success: function(layero){
					table.reload('qualityTable', {
						page: {
							curr: 1 //重新从第 1 页开始
						}
					,where: {
						mqId: da.mqId,
					}
					});
					//渲染
					load();
				},end : function() {
					//弹框关闭的回调函数
					$("#qualityDiv").hide();
				}
			});
		}else if(obj.event === 'Quality'){
			layer.open({
				type: 1	 //此处以iframe举例
				,title: '质检'
					,area: ['1000px', '490px']
			,offset: 'auto'
				,content:$('#qualityUpdateDiv')
				,success: function(layero){
					table.reload('qualityUpdate', {
						page: {
							curr: 1 //重新从第 1 页开始
						}
					});

					var url="../../maqudetail/findAll.action";
					var data={"mqId":da.mqId,"page":1,"limit":100};
					$.post(url,data,function(mes){
						$.each(mes.data,function(index,item){
							$("#qualityBody").append("<tr>" +
									"<td>"+item.mdId+"</td>" +
									"<td>"+item.oper+"</td>" +
									"<td>"+item.mdCount+"</td>" +
									"<td><input class='materNumber' onkeyup='findKeyUp(\""+item.mdId+"\",\""+item.mdCount+"\")' id="+item.mdId+" value="+item.mdNotcount+"></td>"+
									"<td id='alread"+item.mdId+"'>"+item.mdAlready+"</td>"+
									"<td id='lity"+item.mdId+"'>"+item.mdProbability+"</td>"+
									"<td>"+item.optime+"</td>" +
							"</tr>");
							//渲染
							load();
						});
					},"json");
				},end : function() {
					//弹框关闭的回调函数
					$("#qualityUpdateDiv").hide();
					$("#qualityBody").html("");
				}
			});


		}else if(obj.event === 'make'){
			layer.confirm('请确认入库',{title:"确认操作"}, function(indexs){
				var index = layer.load(0, {shade: false});
				var url="../../maqudetail/updateSh.action";
				var data={"mqId":da.mqId};
				$.post(url,data,function(mes){
					layer.close(index);
					if(mes==1){
						parent.layer.msg('入库成功', {
							icon: 1
						});
						//执行重载
						table.reload('idTest', {
							page: {
								curr: 1 //重新从第 1 页开始
							}
						});
					}else{
						parent.layer.msg('入库失败', {
							icon: 2
						});
					}
				},"json");
				layer.close(indexs);
			});
		}

	});
	var $ = layui.$, active = {
			reload: function(){
				var date = $('#date');
				var time = $('#time');
				//执行重载
				table.reload('idTest', {
					page: {
						curr: 1 //重新从第 1 页开始
					}
				,where: {
					date: date.val(),
					time: time.val(),
				}
				});
			},
			findBao:function(){
				var index = layer.load(0, {shade: false});
				var sum="";
				$.each($(".materNumber"),function(index,item){
					sum+=item.id+","+$(item).val()+"@";
				});

				var url="../../maqudetail/updateNotCount.action";
				var data={"sum":sum};
				$.post(url,data,function(mes){
					layer.close(index);
					if(mes==1){
						parent.layer.msg('质检完成', {
							icon: 1
						});
						$("#qualityUpdateDiv").hide();
						exit();
						//执行重载
						table.reload('idTest', {
							page: {
								curr: 1 //重新从第 1 页开始
							}
						});
					}else{
						parent.layer.msg('质检失败', {
							icon: 2
						});
					}
				},"json");
			}
	};

	$('.demoTable .layui-btn').on('click', function(){
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});

	$('.demoTable #insert').on('click', function(){
		layer.open({
			type: 1	 //此处以iframe举例
			,title: '增加类型'
				,area: ['600px', '450px']
		,offset: 'auto'
			,content:$('#addOrUpdate')
			,end : function() {
				$("#operUser").attr({"disabled":false});
				//弹框关闭的回调函数
				$("#addOrUpdate").hide();
				exit();
			}
		});
	});

	$('#sub').on('click', function(){

		var url="../../materials/addOrUpdate.action"; 
		var data=$("#myform").serialize();
		$.post(url,data,function(mes){
			if(mes==1){
				exit();
				$("#addOrUpdate").hide();
				parent.layer.msg('操作成功', {
					icon: 1
				});
				//重载表单
				active['reload'] ? active['reload'].call(this) : '';
			}else{
				parent.layer.msg('操作失败', {
					icon: 2
				});
			}
		},"json");
	});

});
function findKeyUp(id,count){
	var reg=/^\d+(\.\d{0,2})?$/;
	if($("#"+id).val()=='' || isNaN($("#"+id).val())){
		$("#"+id).val(0);
	}else if($("#"+id).val().length>7 || !reg.test($("#"+id).val())){
		$("#"+id).val($("#"+id).val().substring(0,$("#"+id).val().length-1));
	}
	$("#alread"+id).html(parseFloat(count)-parseFloat($("#"+id).val()));
	$("#lity"+id).html((parseFloat($("#alread"+id).html())/parseFloat(count)*100).toFixed()+"%");
}
function load() {
	var form = layui.form
	form.render();
}